import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    products:{}
  },
  getters:{
    
  },
  mutations: {
    initCartProducts(state,data){
      state.products=data;
    },
    setCartComments(store,data){
      if(this.state.products.hasOwnProperty(data.tradeItemId)){
        this.state.products[data.tradeItemId].count +=1;
      }else{
        data.count = 1;
        this.state.products[data.tradeItemId] = data;
      }
      localStorage.setItem("carts",JSON.stringify(this.state.products));
    }
  },
  actions: {
    loadCartData(store){
      let obj = JSON.parse(localStorage.getItem("carts"));
      if(obj){
        store.commit("initCartProducts",obj)
      }else{
        store.commit("initCartProducts",{})
      }
    }
  },
  modules: {

  }
})
